﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PuzzleCoop
{
    public class PlayScreen : GameScreen
    {
        public Board board;

        Cursor mouseCursor;
        public List<GameObject> gameObjects;
        public List<Skill> skills;
        public EffectsManager effectsManager;
        public Player player;

        Point endPoint;

        public PlayScreen()
            : base()
        {
        }

        public override void LoadContent()
        {
            //System.Threading.Thread.Sleep(2000); // FOR TESTS (thread)
            endPoint = new Point(Board.Size.X - 1, Board.Size.Y - 1);
            gameObjects = new List<GameObject>();
            skills = new List<Skill>(); 
            effectsManager = new EffectsManager();
            mouseCursor = new Cursor();
            player = new Player(this, 0);
            board = new Board(this);
            gameObjects.Add(player);
        }

        public override void Update(GameTime gameTime)
        {
            GameOverCheck();
            UpdateInput();
            mouseCursor.Update(gameTime);

            board.Update(gameTime);

            for (int i = 0; i < gameObjects.Count; ++i)
            {
                if (!gameObjects[i].alive) 
                    gameObjects.RemoveAt(i);
                else
                    gameObjects[i].Update(gameTime);
            }
            for (int i = 0; i < skills.Count; ++i)
            {
                if (!skills[i].alive) skills.RemoveAt(i);
                else skills[i].Update(gameTime);
            }

            effectsManager.Update(gameTime);
        }

        private bool MapCollision(GameObject obj)
        {
            Vector2 pos = obj.position;
            /*if (pos.X < mapBound0.X) obj.MapCollision(Direction.Left);
            if (pos.Y < mapBound0.Y) obj.MapCollision(Direction.Up);
            if (pos.X > mapBound1.X) obj.MapCollision(Direction.Right);
            if (pos.Y > mapBound1.Y) obj.MapCollision(Direction.Down);*/
            return true;
        }

        private void GameOverCheck()
        {
            if (board.GetCoord(player.position) == endPoint)
            {
                GameScreenManager.Pop();
                GameScreenManager.Push(new StartScreen());
            }
        }

        private void UpdateInput()
        {
            if (Input.ButtonPressed(0, Buttons.Start)) { GameScreenManager.Pop(); GameScreenManager.Push(new StartScreen()); return; }
            if (Input.ButtonPressed(player.playerIndex, Buttons.Start)) { GameScreenManager.Pop(); GameScreenManager.Push(new StartScreen()); return; }

            // Mouse + Keyboard (for tests)
            // P1 movement by keyboard
            Vector2 dir = Vector2.Zero;
            if (Input.KeyDown(Keys.W)) dir += Direction.Up;
            if (Input.KeyDown(Keys.A)) dir += Direction.Left;
            if (Input.KeyDown(Keys.D)) dir += Direction.Right;
            if (Input.KeyDown(Keys.S)) dir += Direction.Down;
            if (dir != Vector2.Zero)
            {
                dir.Normalize();
                player.PAMove(dir);
            }
            if (Input.KeyPressed(Keys.LeftShift)) skills.Add(new Shot(player));


            //PE
            if (Input.KeyPressed(Keys.Space)) player.SelectSlot();
            if (Input.KeyPressed(Keys.Left)) player.PEMove(new Point(-1, 0));
            if (Input.KeyPressed(Keys.Right)) player.PEMove(new Point(1, 0));
            if (Input.KeyPressed(Keys.Up)) player.PEMove(new Point(0, -1));
            if (Input.KeyPressed(Keys.Down)) player.PEMove(new Point(0, +1));
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            board.DrawA(spriteBatch);
            foreach (GameObject obj in gameObjects) obj.Draw(spriteBatch);
            effectsManager.Draw(gameTime, spriteBatch);
            board.DrawE(spriteBatch);
            player.DrawE(spriteBatch);
            mouseCursor.Draw(spriteBatch);
            
            // escreve na tela a quantidade de objetos
            int x = 0;
            foreach (GameObject go in gameObjects) if (new Rectangle(0, 0, 640, 720).Contains((int)go.drawPosition.X, (int)go.drawPosition.Y)) x++;
            spriteBatch.DrawString(PuzzleCoop.Font, "Game Objects: " + gameObjects.Count.ToString() + "\n On Scene: " + x, new Vector2(1100, 20), Color.White);
        }
    }
}   
